#include "my.h"
struct msgbuf
{
	long mtype;
	char mtext[100];
};

typedef struct
{
	int msgid;
	int sendType;
	int recvType;
}data_t;
void *sendThread(void *p)
{
	struct msgbuf s = { 0 };
	data_t *q = p;
	while(1)
	{
		s.mtype = q->sendType;
		scanf("%s",s.mtext);
		msgsnd(q->msgid,&s,sizeof(s)-sizeof(long),0);
	}
}


void *recvThread(void *p)
{
	data_t *q = p;
	struct msgbuf s = { 0 };
	while(1)
	{
		msgrcv(q->msgid,&s,sizeof(s)-sizeof(long),q->recvType,0);
		printf("%ld:%s\n",s.mtype,s.mtext);
	}
	
}




int main(int argc, const char *argv[])
{
	if(argc != 3)
	{
		printf("忘记传递参数\n");
		exit(-1);
	}
	data_t data = { 0 };
	data.sendType = atoi(argv[1]);
	data.recvType = atoi(argv[2]);
	data.msgid = msgget(77777,0666|IPC_CREAT);
	if(data.msgid == -1)
	{
		perror("msgget failed");
		
		exit(-1);
	}
	pthread_t id1,id2;
	pthread_create(&id1,NULL,sendThread,&data);
	pthread_create(&id2,NULL,recvThread,&data);

	pthread_join(id1,NULL);
	pthread_join(id2,NULL);
	return 0;
}
